const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    dishi: ['----请选择招考地市----'],
    danwei: ['----请选择招考单位----'],
    xingbie: ['----请选择报考性别----', '男', '女'],
    phone: 0,
    theList: ''
  },


  dishiChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('dishi发送选择改变，携带值为', that.data.dishi[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42950&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'dishi',
        grtext: that.data.dishi[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].danwei)
        }
        that.setData({
          danwei: list
        });
      }
    })
  },
  danweiChange: function (e) {
    var that = this;
    console.log('danwei发送选择改变，携带值为', that.data.danwei[e.detail.value])
    this.setData({
      index2: e.detail.value
    })
  },
  xingbieChange: function (e) {
    var that = this;
    console.log('xingbie发送选择改变，携带值为', that.data.xingbie[e.detail.value])
    this.setData({
      index3: e.detail.value
    })
  },
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
        area: options.area,
        geneal: options.agent
    })
    if (options.page) {
        that.setData({
            page: options.page
        })
    }
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42950&callback=?', //接收url
      data: {
        level: '1',
        dishi: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('----请选择地市----')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].dishi)
        }
        that.setData({
          dishi: list
        });
      }
    })

   
  },

  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },


  getPhoneNumber(e) {
    var that = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      var that = this;
      that.setData({
        phone: res.result,
      })
      wx.setStorageSync('phone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/register?actid=42950&callback=?', //接收url
        data: {
          name: that.data.name,
          dishi: that.data.dishi[that.data.index],
          danwei: that.data.danwei[that.data.index2],
          xingbie: that.data.xingbie[that.data.index3],
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          console.log(res)
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)

          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
            method: 'POST',
            data: {
                activeId: 23,
                mobile: res.result,
                areaCode: that.data.area,
                agent: that.data.geneal,
                pageId: that.data.page
            },
            success: res => {
                console.log(res);
            }
        })

          wx.request({ //发送请求
            url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=42950&callback=?",
            data: {
              dishi: that.data.dishi[that.data.index],
              danwei: that.data.danwei[that.data.index2],
              xingbie: that.data.xingbie[that.data.index3],
              limits: '100'
            }, //接收参数
            success(res) { //返回
              var jsonStr = res.data;
              jsonStr = jsonStr.replace("(", "");
              jsonStr = jsonStr.replace(")", "");
              if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
              };
              console.log(theJson)
              if (theJson.status === 1) {
                that.setData({
                  theList: theJson.lists
                })
                console.log(that.data.theList)
              } else {
                wx.showToast({
                  title: '暂无数据！',
                })
              }

            }
          })

        }
      })
      var that = this;
      var dishi = that.data.dishi[that.data.index];
      var danwei = that.data.danwei[that.data.index2];
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/gettotal/?actid=42950&callback=?",
        data: {
          dishi: dishi,
          danwei: danwei,
          xingbie: '男'
        }, //接收参数
        success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          };
          console.log(theJson.counts)
          that.setData({
            redanweiunan: theJson.counts
          })
        }
      })

      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/gettotal/?actid=42950&callback=?",
        data: {
          dishi: dishi,
          danwei: danwei,
          xingbie: '女'
        }, //接收参数
        success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          };
          console.log(theJson.counts)
          that.setData({
            redanweiunv: theJson.counts
          })
        }
      })
    }).catch(err => {
      console.error(err);
    });

  },
  tijiao: function () {

    var that = this;
    var name = that.data.name;
    var dishi = that.data.dishi[that.data.index];
    var danwei = that.data.danwei[that.data.index2];
    var xingbie = that.data.xingbie[that.data.index3];

    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=42950&callback=?",
      data: {
        name: name,
        dishi: dishi,
        danwei: danwei,
        xingbie: xingbie,
        limits: '100'
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson)
        if (theJson.status === 1) {
          that.setData({
            theList: theJson.lists
          })
          console.log(that.data.theList)
        } else {
          wx.showToast({
            title: '暂无数据！',
          })
        }
      }
    })

    var that = this;
    var name = that.data.name;
    var dishi = that.data.dishi[that.data.index];
    var danwei = that.data.danwei[that.data.index2];
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/gettotal/?actid=42950&callback=?",
      data: {
        dishi: dishi,
        danwei: danwei,
        xingbie: '男'
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson.counts)
        that.setData({
          redanweiunan: theJson.counts
        })
      }
    })

    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/chaxun/gettotal/?actid=42950&callback=?",
      data: {
        dishi: dishi,
        danwei: danwei,
        xingbie: '女'
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
        console.log(theJson.counts)
        that.setData({
          redanweiunv: theJson.counts
        })
      }
    })

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  odanweiow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  odanweiareAppMessage: function () {
    var scene = wx.getStorageSync('scene')
    return {
      title: '2021山东农商行报名热度查询',
      path: 'pages/index/index?scene=' + scene + '&by_qr=2'
    }
  },
  /**分享到朋友圈*/
  odanweiareTimeline: function () {
    var that = this;
    var scene = wx.getStorageSync('scene')
    return {
      title: '2021山东农商行报名热度查询',
      path: 'pages/index/index?scene=' + scene + '&by_qr=2'
    }
  }
})